import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class h_6 {
    public static void main(String[] args) throws Exception {
        sortTxt("sort.txt");
    }
    public static void sortTxt(String s) throws Exception {
        BufferedReader fis=new BufferedReader(new FileReader(s));
        List<String> list=new ArrayList<>();
        String str=null;
        while((str=fis.readLine())!=null){
            list.add(str);
        }
        System.out.println(list);
        StringBuffer sb=new StringBuffer();
        list.stream().sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o1.substring(0,1))-Integer.parseInt(o2.substring(0,1));
            }
        }).forEach(a-> sb.append(a+"\n"));
//        fos.write(sb.toString());
        System.out.println(sb.toString());
        BufferedWriter fos=new BufferedWriter(new FileWriter(s));
        fos.write(sb.toString());
        fos.close();
        fis.close();
    }
}
